package cn.dg.manager.warehouse.action;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;

import cn.dg.manager.base.BaseController;
import cn.dg.manager.base.entity.HttpResult;
import cn.dg.manager.base.util.PageData;
import cn.dg.manager.common.entity.PageEntity;
import cn.dg.manager.warehouse.entity.WarehouseDetailResultBean;
import cn.dg.manager.warehouse.entity.WarehouseEditPageBean;
import cn.dg.manager.warehouse.entity.WarehouseInfoResultBean;
import cn.dg.manager.warehouse.entity.WarehousePageParam;
import cn.dg.manager.warehouse.service.WareHouseService;

@Controller
public class WarehouseAction extends BaseController {

	@Autowired
	private WareHouseService wareHouseService;
	
	@RequestMapping(value="/warehouse/index")
	public String index() {
		return "warehouse/main";
	}
	
	/**
	 * 获取商品分类的数据
	 */
	@RequestMapping(value="/warehouse/search", method = RequestMethod.POST)
	@ResponseBody
	public Object getWarehouseInfoList() {
		PageData pd = this.getPageData();
		
		String page = pd.getString("page");
		String rows = pd.getString("rows");
		String updateTime = pd.getString("warehouse_create_time");
		String warehouseName = pd.getString("warehouse_name");
		String goodsName = pd.getString("goods_name");
		// 组装查询参数
		Map<String, Object> mapParams = new HashMap<String, Object>(5);
		mapParams.put("page", page);
		mapParams.put("rows", rows);
		mapParams.put("updateTime", updateTime);
		mapParams.put("warehouseName", warehouseName);
		mapParams.put("goodsName", goodsName);
		
		PageEntity<WarehouseInfoResultBean> result = wareHouseService.getWarehouseListPage(mapParams);
		return result;
	}
	
	/**
	 * 获取入库订单详细信息的数据
	 * 
	 * @return
	 */
	@RequestMapping(value="/warehouse/warehouseDetailList/{warehouseId}", method = RequestMethod.POST)
	@ResponseBody
	public Object getWarehouseDetailInfoList(@PathVariable String warehouseId) {
		Map<String, Object> params = new HashMap<String, Object>(1);
		params.put("warehouseId", warehouseId);
		List<WarehouseDetailResultBean> bean = wareHouseService.getWarehouseDetailInfo(params);
		return bean;
	}
	
	@RequestMapping(value="/warehouse/add")
	public ModelAndView add() {
		ModelAndView mv = this.getModelAndView();
		mv.addObject("mode", "0");
		mv.setViewName("warehouse/detail");
		return mv;
	}
	
	/**
	 * 跳转至新增培训信息管理编辑窗口detail.jsp
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value="/warehouse/showAddress")
	public ModelAndView address() {
		PageData pd = this.getPageData();
		String address_id = pd.getString("address_id");
		ModelAndView mv = this.getModelAndView();
		mv.addObject("s_address_id", address_id);
		mv.setViewName("warehouse/address");
		return mv;
	}
	
	/**
	 * 入库更新
	 * 
	 * @param warehouseId
	 * @return
	 */
	@RequestMapping(value="/warehouse/update/{warehouseId}")
	public ModelAndView update(@PathVariable String warehouseId) {
		ModelAndView mv = this.getModelAndView();
		// 更新
		mv.addObject("mode", "1");
		mv.addObject("warehouse_id", warehouseId);
		mv.setViewName("warehouse/detail");
		return mv;
	}
	
	/**
	 * 获取编辑的入库订单数据
	 * 
	 * @param warehouseId
	 * @return
	 */
	@RequestMapping(value="/warehouse/getEditWarehouseInfoData/{warehouseId}",method=RequestMethod.POST)
	@ResponseBody
	public Object getEditWarehouseInfoData(@PathVariable String warehouseId) {
		WarehouseEditPageBean rslt = wareHouseService.getEditWarehouseInfoData(warehouseId);
		return HttpResult.createSuccessResult(rslt, "0");
	}
	
	@RequestMapping(value="/warehouse/saveOrUpdate")
	@ResponseBody
	public  Object saveOrUpdate () {
		PageData pd = this.getPageData();
		WarehousePageParam bean = new WarehousePageParam();
		String warehouseId = pd.getString("warehouseId");
		if(!StringUtils.isEmpty(warehouseId)) {
			bean.setWarehouseId(Integer.valueOf(warehouseId));
		}
		
		bean.setWarehouseName(pd.getString("warehouseName"));
		if(!StringUtils.isEmpty(pd.getString("totalPrice"))) {
			bean.setTotalPrice(Integer.valueOf(pd.getString("totalPrice")));
		}
		if(!StringUtils.isEmpty(pd.getString("totalPriceJp"))) {
			bean.setTotalPriceJp(Integer.valueOf(pd.getString("totalPriceJp")));
		}
		if(!StringUtils.isEmpty(pd.getString("total_cnt"))) {
			bean.setTotalCnt(Integer.valueOf(pd.getString("total_cnt")));
		}
		if(!StringUtils.isEmpty(pd.getString("addressId"))) {
			bean.setAddressId(Integer.valueOf(pd.getString("addressId")));
		}
		bean.setUserName(pd.getString("userName"));
		bean.setTelPhone(pd.getString("telPhone"));
		bean.setPostCode(pd.getString("postCode"));
		bean.setAddress(pd.getString("address"));
		bean.setWarehouseDetailJsonObject(pd.get("warehouseDetailJsonObject"));
		
		long rslt = 0L;
		if(StringUtils.isEmpty(warehouseId)) {
			rslt = wareHouseService.insertWarehouseInfo(bean);
		} else {
			rslt = wareHouseService.updateWareHouseInfo(bean);
		}
		if(rslt > 0) {
			return HttpResult.createSuccessResult("", "0");
		} else {
			return HttpResult.createErrorResult("创建入库订单失败！", "", "-1");
		}
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="/warehouse/del")
	@ResponseBody
	public  Object delete () {
		PageData pd = this.getPageData();
		long rslt = wareHouseService.deleteWareHouseInfo(Arrays.asList(pd.getString("warehouse_id").split(",")));
		if(rslt > 0) {
			return HttpResult.createSuccessResult("", "0");
		} else {
			return HttpResult.createErrorResult("删除入库订单失败！", "", "-1");
		}
	}
	
	/**
	 * 根据订单号导出订单文件 
	 * 
	 * @param orderId 订单号
	 */
	@RequestMapping(value="/warehouse/exportExcelFile/{wareHouseId}")
	public void exportExcelFile(@PathVariable String wareHouseId, HttpServletResponse response) {
		wareHouseService.exportExcelFile(wareHouseId, response);
	}
}
